package per.sunmes.cl.g.actor;

import per.sunmes.cl.g.data.CardData;
import per.sunmes.cl.g.sys.U;

import com.badlogic.gdx.graphics.Color;
import com.badlogic.gdx.math.Vector2;
import com.badlogic.gdx.scenes.scene2d.Group;
import com.badlogic.gdx.scenes.scene2d.actions.Actions;
import com.badlogic.gdx.scenes.scene2d.ui.Image;
import com.badlogic.gdx.scenes.scene2d.ui.Label;
import com.badlogic.gdx.scenes.scene2d.ui.Label.LabelStyle;
import com.badlogic.gdx.scenes.scene2d.utils.Drawable;
import com.badlogic.gdx.utils.Align;
import com.badlogic.gdx.utils.Scaling;

public class Card extends Group {

	Image bg, bbg, boxName, boxPic, boxTxt;
	Group fg = new Group();
	Group smallShow = new Group();

	Label labelName, labelTxt;
	Image pic;

	Player player;
	CardData cardData;
	Vector2 orderPos = new Vector2();

	public Card() {
		addActor(fg);
		initFG();

		bbg = U.image("dialog/card_back_1.png");
		addActor(bbg);
		bbg.setSize(getWidth(), getHeight());

		addActor(smallShow);
		smallShow.setVisible(false);

		showFrontPage();
	}

	public Vector2 getOrderPos() {
		return orderPos;
	}

	public void updateSize() {
		fg.setScale(getWidth() / bg.getWidth());
		bbg.setScale(fg.getScaleX());

		smallShow.setOrigin(-boxPic.getX(), -boxPic.getY());
		smallShow.setScale(fg.getScaleX());
	}

	public Image getBbg() {
		return bbg;
	}

	public Image getBg() {
		return bg;
	}

	public void changeBackBg(Drawable draw) {
		bbg.setDrawable(draw);
	}

	public boolean isFrontPage() {
		return fg.isVisible();
	}

	public void showFrontPage() {
		fg.setVisible(true);
		bbg.setVisible(false);
	}

	public void showBackPage() {
		fg.setVisible(false);
		bbg.setVisible(true);
	}

	private void initFG() {
		bg = U.image("dialog/card_base.png");
		fg.addActor(bg);
		setSize(bg.getWidth(), bg.getHeight());

		boxName = U.image("dialog/card_name.png");
		fg.addActor(boxName);
		boxName.setPosition(getWidth() / 2, getHeight() - 13, Align.top);
		U.disTouch(boxName);

		boxPic = U.image("dialog/card_pic.png");
		fg.addActor(boxPic);
		boxPic.setPosition(getWidth() / 2, boxName.getY() - 2, Align.top);
		U.disTouch(boxPic);
		smallShow.setSize(boxPic.getWidth(), boxPic.getHeight());
		smallShow.setPosition(boxPic.getX(), boxPic.getY());

		boxTxt = U.image("dialog/card_txt.png");
		fg.addActor(boxTxt);
		boxTxt.setPosition(getWidth() / 2, boxPic.getY() - 2, Align.top);
		U.disTouch(boxTxt);

		labelName = new Label("卡牌名", new LabelStyle(U.getCardNameFont(),
				Color.WHITE));
		fg.addActor(labelName);
		labelName.setAlignment(Align.center);
		U.center(labelName, boxName);
		U.disTouch(labelName);

		labelTxt = new Label("卡牌描述", new LabelStyle(U.getCardTxtFont(),
				Color.BLACK));
		fg.addActor(labelTxt);
		labelTxt.setSize(boxTxt.getWidth() - 20, boxTxt.getHeight() - 20);
		labelTxt.setAlignment(Align.center);
		labelTxt.setWrap(true);
		U.center(labelTxt, boxTxt);

	}

	public void setCardData(CardData cardData) {
		this.cardData = cardData;
	}

	public CardData getCardData() {
		return cardData;
	}

	public void updateCardData() {
		labelName.setText(cardData.getName());
		labelTxt.setText(cardData.getDescription());
		if (labelTxt.getPrefHeight() > labelTxt.getHeight()) {
			labelTxt.setFontScale(labelTxt.getHeight()
					/ labelTxt.getPrefHeight());
		} else if (labelTxt.getFontScaleY() != 1) {
			labelTxt.setFontScale(1);
		}
		if (pic != null) {
			pic.remove();
		}
		pic = U.image(cardData.getPicPath());
		U.disTouch(pic);
		fg.addActor(pic);
		if (pic.getWidth() > boxPic.getWidth()
				|| pic.getHeight() > boxPic.getHeight()) {
			pic.setSize(boxPic.getWidth(), boxPic.getHeight());
			pic.setScaling(Scaling.fit);
		}
		U.center(pic, boxPic);
	}

	public Player getPlayer() {
		return player;
	}

	public void setPlayer(Player player) {
		this.player = player;
	}

	public void updateToPlayerCard() {
		if (player == null) {
			bg.setColor(Color.WHITE);
		} else {
			bg.setColor(player.getPlayerColor());
		}
	}

	@Override
	public String toString() {
		return String.format("Card[%d]{Player[%s],Data[%s]}", hashCode(),
				player, cardData);
	}

	public void changeToSmallPic() {
		fg.clearActions();
		bbg.clearActions();
		fg.addAction(Actions.fadeOut(0.2f));
		bbg.addAction(Actions.fadeOut(0.2f));

		smallShow.setVisible(true);
		smallShow.clear();
		Image sspic;
		if (pic != null) {
			sspic = new Image(pic.getDrawable());
		} else {
			sspic = new Image(boxPic.getDrawable());
		}
		sspic.setSize(smallShow.getWidth(), smallShow.getHeight());
		sspic.setScaling(Scaling.fit);
		smallShow.addActor(sspic);
	}

	public void changeBackToCard() {
		fg.clearActions();
		bbg.clearActions();
		fg.addAction(Actions.fadeIn(0.2f));
		bbg.addAction(Actions.fadeIn(0.2f));

		smallShow.addAction(Actions.sequence(Actions.delay(0.2f),
				Actions.visible(false)));
	}

	public boolean isCouldPutToZone(Zone zone) {
		return cardData.getEffect().isCouldPutToZone(zone);
	}

}
